package com.mes.eventhandler;

import com.mes.manufacture.data.EmesoManuorderMaterial;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;

public class EmesoManuorderMaterialEventHandler extends EntityPersistenceEventObserver {
	
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EmesoManuorderMaterial.ENTITY_NAME) };

	protected Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	protected Entity[] getObservedEntities() {
		// TODO Auto-generated method stub
		return entities;
	}
	
	private void throwMessage(String message){
		String language = OBContext.getOBContext().getLanguage().getLanguage();
		ConnectionProvider conn = new DalConnectionProvider(false);
		throw new OBException(Utility.messageBD(conn, message, language));
	}
	
	public void onSave(@Observes EntityNewEvent event) {
	    if (!isValidEvent(event)) {
	      return;
	    }
	    final EmesoManuorderMaterial emesoManuorderMaterial = (EmesoManuorderMaterial) event.getTargetInstance();
	    String status = emesoManuorderMaterial.getEmesoManuorder().getStatus();
	    String product = emesoManuorderMaterial.getProduct().getId();
	    double  qty = emesoManuorderMaterial.getQty().doubleValue();
	    vilidateData(product, qty);
	    if(!status.equalsIgnoreCase("create")){
	    	throwMessage("未审批领料单才能进行新增物料数据！");
	    }
	}

	public void onUpdate(@Observes EntityUpdateEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesoManuorderMaterial emesoManuorderMaterial = (EmesoManuorderMaterial) event.getTargetInstance();
	    String status = emesoManuorderMaterial.getEmesoManuorder().getStatus();
	    String product = emesoManuorderMaterial.getProduct().getId();
	    double qty = emesoManuorderMaterial.getQty().doubleValue();
	    vilidateData(product, qty);
	    if(!status.equals("create")) {
	    	throwMessage("未审批领料单才能进行修改物料数据！");
	    }
	}
	
	public void onDelete(@Observes EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		final EmesoManuorderMaterial emesoManuorderMaterial = (EmesoManuorderMaterial) event.getTargetInstance();
	    String status = emesoManuorderMaterial.getEmesoManuorder().getStatus();
	    if(!status.equals("create")){
	    	throwMessage("未审批领料单才能进行删除物料数据！");
	    }
	}
	
	private void vilidateData(String product,double qty) {
		ConnectionProvider conn = new DalConnectionProvider(false);
		String language = OBContext.getOBContext().getLanguage().getLanguage();
		String sql = "select nvl(sum(qty)-sum(qty_lock),0) qty from wms.wms_storage_detail where pcode = ? and wname = '电池原材料库'";
		Query query = OBDal.getInstance().getSession().createSQLQuery(sql);
		query.setString(0, product);
		double qtyIn = Double.parseDouble(query.uniqueResult().toString());
		if (qty > qtyIn) {
			throw new OBException(Utility.messageBD(conn, "库存数量不足，无法添加，当前物料可用库存为" + qtyIn + "！", language));
		}
	}
	
	
}
